package com.keeson.android_cli.presenter;


import com.keeson.android_cli.api.Api;
import com.keeson.android_cli.base.BasePresenterImpl;
import com.keeson.android_cli.base.BaseRespose;
import com.keeson.android_cli.base.retrofit.BaseObserver;
import com.keeson.android_cli.base.retrofit.RxSchedulers;
import com.keeson.android_cli.bean.PressureInfo;
import com.keeson.android_cli.bean.request.ModifyBedMattressRequest;
import com.keeson.android_cli.entity.BedBindInfo;
import com.keeson.android_cli.entity.UserInfo;
import com.keeson.android_cli.interfaces.contract.MainContract;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by jiaqiZHou
 * on 2020/12/4
 */
public class MainPresenter extends BasePresenterImpl<MainContract.View> implements MainContract.Presenter {

    public MainPresenter(MainContract.View view) {
        super(view);
    }


    @Override
    public void refreshToken(int uid) {
       // view.showProgress(null);
        Api.getInstance().refreshToken(uid)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<UserInfo>>() {
                    @Override
                    protected void onSuccess(BaseRespose<UserInfo> data) {
                        //view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.refreshTokenSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        //view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void getUserBindInfo(int uid) {
        Api.getInstance().getUserBindInfo(uid)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<BedBindInfo>>() {
                    @Override
                    protected void onSuccess(BaseRespose<BedBindInfo> data) {
                        //view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.getUserBindInfoSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        //view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void deleteUserBindInfo(Map<String, Object> map) {
        view.showProgress(null);
        Api.getInstance().deleteUserBindInfo(map)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.deleteUserBindInfoSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void modifyUserBindInfos(ModifyBedMattressRequest postBody) {
        view.showProgress(null);
        Api.getInstance().modifyUserBindInfos(postBody)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.modifyUserBindInfosSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void modifyUserMemoryName(Map<String, Object> map) {
        view.showProgress(null);
        Api.getInstance().modifyUserMemoryName(map)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<String>>() {
                    @Override
                    protected void onSuccess(BaseRespose<String> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.modifyUserMemoryNameSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }

    @Override
    public void getPressureInfo(String hardware_version) {
        //view.showProgress(null);
        Api.getInstance().getPressureInfo(hardware_version)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<ArrayList<PressureInfo>>>() {
                    @Override
                    protected void onSuccess(BaseRespose<ArrayList<PressureInfo>> data) {
                        //view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.getPressureInfoNameSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        //view.dismissProgress();
                        super.onError(msg, errorCode);
                    }
                });
    }
}